/**
 * Created by admin on 2017/10/23.
 */
$(document).ready(function () {
    $("form").submit(function () {
        return false;
    });
    var VAL = {
        NORMAL: "0000000",
        EVT_USER: $("#userId").text(),
        T_FDPAPLYWO: "FDPAPLYWO",
        T_FBPMDLDEF: "FBPMDLDEF",
        T_FPPUPKCLM: "FPPUPKCLM",
        T_FIPINQSHT: "FIPINQSHT",
        T_FIPINQBOX: "FIPINQBOX",
        DISABLED_ATTR: {
            'disabled': true
        },
        ENABLED_ATTR: {
            'disabled': false
        }
    };
    var domObj = {

        $query_btn: $("#query_btn"),
        $save_btn: $("#save_btn"),
        $clean_btn: $("#clean_btn"),
        $cancel_btn: $("#cancel_btn"),
        $wrls_btn: $("#wrls_btn"),
        $clean_prd_btn:$("#clean_prd_btn"),
        tempProdListDiv: $("#tempProdListDiv"),
        tempProdListGrd: $("#tempProdListGrd"),
        tempProdListPg: "#tempProdListPg",

        $woCateSel: $("#woCateSel"),
        $woIdSel: $("#woIdSel"),

        $mdlIdFk: $("#mdlIdFk"),
        $mtrlProdId: $("#mtrlProdId"),
        $outLineMount: $("#outLineMount"),
        $lineMout: $("#lineMout"),
        $mtrlBoxId: $("#mtrlBoxId"),

        $productId: $("#productId"),
        $lotIdTxt: $("#lotIdTxt"),
        $reasonCode: $("#reasonCode"),
        $radioOK: $("#radioOK"),
        $radioNG: $("#radioNG"),
        $pathId: $("#pathId")


    };
    var iniGridInfo = function () {
        var tempProdListCM = [
            {name: 'wo_id_fk', index: 'wo_id_fk', label: WO_ID_TAG, width: 160},
            {name: 'prd_seq_id', index: 'prd_seq_id', label: SHEET_ID_TAG, width: 200},
            {name: 'prd_grade', index: 'prd_grade', label: '产品等级', width: 60},
            {name: 'mdl_id_fk', index: 'mdl_id_fk', label: PRODUCT_ID_TAG, width: 160},
            {name: 'prd_stat', index: 'prd_stat', label: '产品状态', width: 160},
            //{name: 'so_id_fk', index: 'so_id_fk', label: '内部订单号', width: 100},
            {name: 'evt_usr', index: 'evt_usr', label: EVT_USR, width: 100},
            {name: 'evt_timestamp', index: 'evt_timestamp', label: EVT_TIMESTAMP, width: 200},
            {name: 'mtrl_box_id', index: 'mtrl_box_id', label: "来料箱号", width: 200, hidden:true},
            {name: 'prd_act_qty', index: 'prd_act_qty', label: "剩余数量", width: 100, hidden:true}
        ];
        domObj.tempProdListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            height: 200,
            width: domObj.tempProdListDiv.width(),
            autowidth: false,
            shrinkToFit: false,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            rowNum: 20,
            emptyrecords: true,
//            multiselect : true,
            pager: domObj.tempProdListPg,
            colModel: tempProdListCM
        });

    };
    var toolFunc = {
        iniWoCateSel: function () {
            comAddValueByDataCateFnc("#woCateSel", "WOCT", "data_ext", "data_desc", true);
        },
        iniWoIdSel: function () {
            domObj.$mdlIdFk.val("");
            domObj.$mtrlProdId.val("");
            domObj.$outLineMount.val("");
            domObj.$lineMout.val("");
            var wo_cate = domObj.$woCateSel.find("option:selected").val();
            var inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: "Q",
                iary: [{
                    wo_cate: wo_cate
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oaryA = outObj.oary;
                var compare = function (obj1, obj2) {
                    var val1 = obj1.wo_id;
                    var val2 = obj2.wo_id;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                oaryA.sort(compare);
                SelectDom.addSelectArr(domObj.$woIdSel, oaryA, "wo_id", "wo_id", "", true);
            }
        },
        iniNgCode: function () {
            comAddValueByDataCateFnc("#reasonCode", "DFCT", "data_id", "data_ext", true);
        },
        setWoInfo: function () {
            var wo_id = domObj.$woIdSel.find("option:selected").val();
            if (!wo_id) {
                domObj.$mdlIdFk.val("");
                domObj.$mtrlProdId.val("");
                domObj.$outLineMount.val("");
                domObj.$lineMout.val("");
                domObj.$pathId.val("");
                return false;
            }
            var inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: "W",
                wo_id: wo_id
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                domObj.$mdlIdFk.val("");
                domObj.$mtrlProdId.val("");
                domObj.$outLineMount.val("");
                domObj.$lineMout.val("");
                oary = outObj.oary[0];
                if (!oary) {
                    return;
                }
                domObj.$pathId.val(oary.path_id_fk);
                var mdl_id_fk = oary.mdl_id_fk.trim();
                domObj.$mdlIdFk.val(mdl_id_fk);
                domObj.$lineMout.val(oary.pln_prd_qty);
                domObj.$outLineMount.val(oary.rl_prd_qty);
                domObj.$mtrlProdId.val(oary.mtrl_prod_id_fk);
                // var inTrxObj ={
                //     trx_id     : VAL.T_FBPMDLDEF,
                //     action_flg : 'Q'        ,
                //     iary       :[{
                //         mdl_id	: mdl_id_fk
                //     }]
                // };
                // var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                // if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                //     oary = outTrxObj.oary[0];
                //     if(!oary){
                //         return;
                //     }
                //     domObj.$mtrlProdId.val("");
                // }
            }
        },
        radioNgFnc: function () {
            $("#reasonCode").attr(VAL.ENABLED_ATTR);
        },
        radioOkFnc: function () {
            $("#reasonCode").attr(VAL.DISABLED_ATTR);
        },
        checkbox_func: function () {
            var mtrl_box_id = domObj.$mtrlBoxId.val().trim();
            if (!mtrl_box_id) {
                showErrorDialog("", "来料箱号不能为空");
                return false;
            }
            var inObj = {
                trx_id: VAL.T_FIPINQBOX,
                action_flg: 'E',
                mtrl_box_id: mtrl_box_id
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.mtrl_prod_id_fk != domObj.$mtrlProdId.val().trim()) {
                    showErrorDialog("", "来料箱的来料型号与工单的来料型号不符");
                    domObj.$mtrlBoxId.val("");
                    return false;
                }
                if (parseInt(outObj.prd_tl_qty, 10) == 0) {
                    showErrorDialog("", "来料箱可用玻璃为0");
                    domObj.$mtrlBoxId.val("");
                    return false;
                }
            }
        }
    };
    var btnFunc = {
        query_func: function () {
            var actionFlg, inObj, outObj, iary;
            var woId, prdId, lotId,woCate;
            woCate = $.trim(domObj.$woCateSel.val());
            woId =   $.trim(domObj.$woIdSel.val());
            prdId =  $.trim(domObj.$productId.val());
            lotId =  $.trim(domObj.$lotIdTxt.val());
            if (!woId) {
                showErrorDialog("", "工单号不能为空");
                return false;
            }
            iary = {
                wo_id: woId,
                prd_seq_id: prdId,
                prd_stat: 'WAIT',
                lot_id: lotId
            }
            inObj = {
                trx_id: VAL.T_FPPUPKCLM,
                action_flg: 'U',
                iary: [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, "#tempProdListGrd");
            }
        },

        save_func: function () {
            var iary, newRowId;
            var woId, prdId, mtrlProdId, lotId, prdStat, resCode, mtrlBoxId;
            woId = domObj.$woIdSel.val().trim();
            prdId = domObj.$productId.val().trim();
          /*  lotId = domObj.$lotIdTxt.val().trim();*/
            mtrlProdId = domObj.$mtrlProdId.val().trim();
            mtrlBoxId = domObj.$mtrlBoxId.val().trim();
            if (domObj.$radioOK.prop('checked')) {
                prdStat = 'OK';
            } else {
                prdStat = 'NG';
                resCode = domObj.$reasonCode.val().trim();
            }
            if (!woId) {
                showErrorDialog("", "工单号不能为空");
                return false;
            }
//            if (!mtrlBoxId) {
//                showErrorDialog("", "来料箱号不能为空");
//                return false;
//            }
            if (!prdId) {
                showErrorDialog("", "玻璃代码不能为空");
                return false;
            }
            if (domObj.$radioNG.prop('checked') && !resCode) {
                showErrorDialog("", "产品为NG时，缺陷代码不能为空");
                return false;
            }
            iary = {
                wo_id: woId,
                prd_seq_id: prdId,
                lot_id: lotId,
                prd_grade: prdStat,
                mtrl_id_fk: mtrlProdId,
                rsn_code: resCode,
                mtrl_box_id: mtrlBoxId
            }
            var inObj = {
                trx_id: VAL.T_FPPUPKCLM,
                action_flg: 'A',
                evt_usr: VAL.EVT_USER,
                iary: [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                newRowId = getGridNewRowID("#tempProdListGrd");
                domObj.tempProdListGrd.jqGrid("addRowData", newRowId, outObj.oary[0]);
                domObj.tempProdListGrd.setSelection(newRowId, true);
                domObj.$outLineMount.val(outObj.rl_prd_qty);
                $("#productId").val("");
                $("#reasonCode").empty();
                toolFunc.iniNgCode();
            }
        },
        clean_func: function () {
            $("input").val("");
            $("select").empty();
            domObj.tempProdListGrd.jqGrid("clearGridData");
            toolFunc.iniWoCateSel();
            toolFunc.iniWoIdSel();
            toolFunc.iniNgCode();
        },
        cancel_func: function () {
            var inObj, outObj, iary, selRowId, rowData;
            selRowId = $('#tempProdListGrd').jqGrid('getGridParam', 'selrow');
            rowData = domObj.tempProdListGrd.jqGrid("getRowData", selRowId);
            if (rowData.prd_seq_id == null) {
                showErrorDialog("", "请选择一条玻璃记录");
                return false;
            }
            var prdSeqId = rowData.prd_seq_id;
            iary = {
                prd_seq_id: prdSeqId,
            }
            inObj = {
                trx_id: VAL.T_FPPUPKCLM,
                evt_usr: VAL.EVT_USER,
                action_flg: 'D',
                iary: [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("取消成功");
                domObj.tempProdListGrd.jqGrid("delRowData", selRowId);
                toolFunc.setWoInfo();
            }
        },
        clean_prd_btn:function(){
            domObj.tempProdListGrd.jqGrid("clearGridData");
        },
        wholeRls_func: function () {
            var iary, newRowId;
            var woId, prdId, mtrlProdId, lotId, prdStat, resCode, mtrlBoxId;
            woId = domObj.$woIdSel.val().trim();
            prdId = domObj.$productId.val().trim();
            lotId = domObj.$lotIdTxt.val().trim();
            mtrlProdId = domObj.$mtrlProdId.val().trim();
            mtrlBoxId = domObj.$mtrlBoxId.val().trim();
            if (domObj.$radioOK.prop('checked')) {
                prdStat = 'OK';
                $("#mtrlBoxId").empty();
            } else {
                prdStat = '';
                resCode = domObj.$reasonCode.val().trim();
            }
            if (!woId) {
                showErrorDialog("", "工单号不能为空");
                return false;
            }
            if (!mtrlBoxId) {
                showErrorDialog("", "来料箱号不能为空");
                return false;
            }
            if (domObj.$radioNG.prop('checked') && !resCode) {
                showErrorDialog("", "产品为NG时，缺陷代码不能为空");
                return false;
            }
            iary = {
                wo_id: woId,
                lot_id: lotId,
                prd_grade: prdStat,
                mtrl_id_fk: mtrlProdId,
                rsn_code: resCode,
                mtrl_box_id: mtrlBoxId
            }
            var inObj = {
                trx_id: VAL.T_FPPUPKCLM,
                action_flg: 'W',
                evt_usr: VAL.EVT_USER,
                iary: [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                // newRowId = getGridNewRowID("#tempProdListGrd");
                // domObj.tempProdListGrd.jqGrid("addRowData", newRowId, outObj.oary[0]);
                // domObj.tempProdListGrd.setSelection(newRowId, true);
                // domObj.$outLineMount.val(outObj.rl_prd_qty);
                // $("#productId").val("");
                // $("#reasonCode").empty();
                // toolFunc.iniNgCode();
                //todo 查询整箱下线玻璃
            }
            $("#mtrlBoxId").empty();
        }
    }
    var iniContorlData = function () {
        $("input").val("");
        $("textarea").val("");
        toolFunc.iniWoCateSel();
        toolFunc.iniWoIdSel();
        toolFunc.iniNgCode();
        domObj.$woCateSel.change(function () {
            toolFunc.iniWoIdSel();
        });
        domObj.$woIdSel.change(function () {
            toolFunc.setWoInfo();
        });
    };
    var iniButtonAction = function () {
        domObj.$productId.keydown( function (event) {
            if (event.keyCode == 13) {
                var prdId, inObj, outObj, iary;
                prdId = domObj.$productId.val().trim();
                iary = {}
                inObj = {
                    trx_id: VAL.T_FIPINQSHT,
                    action_flg: 'Q',
                    prd_seq_id: prdId,
                    iary: [iary]
                };
                var outObj = comTrxSubSendPostJson(inObj, 'N');
                if (outObj.prd_seq_id != null) {
                    // var oary = new Array();
                    // oary[0] = outObj;
                    // var  newRowId = getGridNewRowID("#tempProdListGrd");
                    // domObj.tempProdListGrd.jqGrid("addRowData", newRowId,oary[0]);
                    // domObj.tempProdListGrd.setSelection(newRowId, true);
                    showErrorDialog("", "该玻璃已经解封下线,请进行取消操作");
                    return false;
                } else {
                    btnFunc.save_func();
                }
            }
        });
    }
    domObj.$radioNG.bind('click', toolFunc.radioNgFnc);
    domObj.$radioOK.bind('click', toolFunc.radioOkFnc);
    domObj.$query_btn.bind('click', btnFunc.query_func);
    domObj.$save_btn.bind('click', btnFunc.save_func);
    domObj.$clean_btn.bind('click', btnFunc.clean_func);
    domObj.$cancel_btn.bind('click', btnFunc.cancel_func);
    domObj.$clean_prd_btn.click(function () {
        btnFunc.clean_prd_btn();
    });
    domObj.$wrls_btn.click(function () {
        btnFunc.wholeRls_func();
    });
    domObj.$mtrlBoxId.keydown(function (event) {
        'use strict';
        if (event.keyCode === 13) {
            toolFunc.checkbox_func();
        }
        return true;
    });
    $(".btn-success").keydown();

    var initializationFunc = function () {
        iniGridInfo();
        iniButtonAction();
        iniContorlData();
    };
    initializationFunc();

    //表格自适应
    function resizeFnc() {
        var offsetBottom, grdDivWidth;
        grdDivWidth = $("#divTop").width();
        offsetBottom = $(window).height() - $("#tempProdListDiv").offset().top;

        $("#tempProdListDiv").width(grdDivWidth * 0.98);
        $("#tempProdListDiv").height(offsetBottom * 0.99);
        $("#tempProdListGrd").setGridWidth(grdDivWidth * 0.97);
        $("#tempProdListGrd").setGridHeight(offsetBottom * 0.95 - 51);
    };
    resizeFnc();
    $(window).resize(function () {
        resizeFnc();
    });
});
